﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TienIch.Utils;
using TienIch.Business;
using TienIch.Model;
using DevExpress.XtraEditors;

namespace TienIch
{
    public partial class frmStaffManager : FormBase
    {
        public frmStaffManager()
        {
            InitializeComponent();
            loadGrid();
        }

        void loadGrid()
        {
            DataTable dt = Easy.Select("Select * from vUserInfo WITH(NOLOCK)");
            grdData.DataSource = null;
            grdData.DataSource = dt;
            grvData.BestFitColumns();
            NhomPhong.DataSource = Easy.Select("SELECT * FROM UserGroup");
            NhomPhong.ValueMember = "ID";
            NhomPhong.DisplayMember = "Name";
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                Easy.ExportExcel(grvData);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, Easy.Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }     
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmShowStaff frm = new frmShowStaff();
            if (frm.ShowDialog()==DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            int id = Easy.ToInt(grvData.GetFocusedRowCellValue(colID));
            if (id == 0) return;
            UsersModel model = (UsersModel)UsersBO.Instance.FindByPKBase(id);
            
            frmShowStaff frm = new frmShowStaff();
            frm.Model = model;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Bạn có thực sự muốn xóa nhân viên này?", Easy.Caption, MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                int id = Easy.ToInt(grvData.GetFocusedRowCellValue(colID));
                if (id == 0) return;
                UsersBO.Instance.Delete(id);
                loadGrid();
            }       
        }

        private void grdData_DoubleClick(object sender, EventArgs e)
        {
            //btnEdit_Click(null, null);
        }

        private void frmStaffManager_Load(object sender, EventArgs e)
        {
            Easy.SetContainsFilter(grvData);
        }

        private void btnQLNhom_Click(object sender, EventArgs e)
        {
            frmStaffGroup frm = new frmStaffGroup();
            frm.Show();
        }
    }
}
